<template>
  <el-dialog
    center
    top="10vh"
    width="800px"
    :visible.sync="dialogShow.value"
    :close-on-click-modal="false"
    v-loading="loading"
    element-loading-background="rgba(0, 0, 0, 0.7)"
    element-loading-text="保存中"
  >
    <span slot="title" class="diy-dialog-title"> 查看单位信息 </span>
    <div class="body-container diy-dialog-body">
      <el-form ref="form" disabled :model="form" :rules="rules" label-position="top">
        <el-form-item
          v-for="item in formItem"
          v-bind="item"
          :key="item.prop"
          :label-width="options.labelWidth"
        >
          <el-input
            v-if="item.prop === 'unitName'"
            v-model="form.unitName"
          ></el-input>
          <el-input
            v-if="item.prop === 'linkPhone'"
            v-model="form.linkPhone"
          ></el-input>
          <el-input
            v-if="item.prop === 'corporateRepresentative'"
            v-model="form.corporateRepresentative"
          ></el-input>
          <el-input
            disabled
            v-if="item.prop === 'administrationDivisionCode'"
            v-model="form.administrationDivisionCode"
          ></el-input>
          <el-cascader
            v-if="item.prop === 'administrationDivisionName'"
            v-model="administrationDivisionName"
            :show-all-levels="false"
            :options="provinces"
            :props="propsName"
          >
          </el-cascader>
          <el-input
            disabled
            v-if="item.prop === 'unitCode'"
            v-model="form.unitCode"
          ></el-input>
          <el-input
            prop="socialCreditCode"
            v-if="item.prop === 'socialCreditCode'"
            v-model="form.socialCreditCode"
          ></el-input>
          <el-select
            v-if="item.prop === 'isVirtual'"
            v-model="form.isVirtual"
            placeholder="请选择虚/实单位"
          >
            <el-option
              v-for="item in isVirtualOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-cascader
            v-if="item.prop === 'value'"
            v-model="value"
            :options="provinces"
            :props="props"
            @change="handleChange"
          >
          </el-cascader>
          <el-input
            v-if="item.prop === 'detailAddress'"
            v-model="detailAddress"
          ></el-input>
          <el-select
            v-if="item.prop === 'organizationForm'"
            v-model="form.organizationForm"
            placeholder="请选择组织形式"
          >
            <el-option
              v-for="item in organizationOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select
            v-if="item.prop === 'unitType'"
            v-model="form.unitType"
            placeholder="请选择单位分类"
          >
            <el-option
              v-for="item in unitTypeOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            v-if="item.prop === 'localFinancialCode'"
            v-model="form.localFinancialCode"
          ></el-input>
          <el-input
            v-if="item.prop === 'localFinancialName'"
            v-model="form.localFinancialName"
          ></el-input>
          <el-input
            v-if="item.prop === 'officeAdministrationCode'"
            v-model="form.officeAdministrationCode"
          ></el-input>
          <el-input
            v-if="item.prop === 'officeAdministrationName'"
            v-model="form.officeAdministrationName"
          ></el-input>
          <el-input
            v-if="item.prop === 'budgetCode'"
            v-model="form.budgetCode"
          ></el-input>
          <el-input
            v-if="item.prop === 'unitTypeName'"
            v-model="form.unitTypeName"
          ></el-input>
          <el-select
            v-if="item.prop === 'unitBasicProperty'"
            v-model="form.unitBasicProperty"
            placeholder="请选择单位基本性质"
          >
            <el-option
              v-for="item in unitBasicPropertyOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select
            v-if="item.prop === 'budgetUnitName'"
            v-model="form.budgetUnitName"
            placeholder="请选择预算单位级次名称"
          >
            <el-option
              v-for="item in budgetUnitNameOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select
            v-if="item.prop === 'budgetManagementLevel'"
            v-model="form.budgetManagementLevel"
            placeholder="请选择预算管理级次"
          >
            <el-option
              v-for="item in budgetManagementLevelOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

          <el-input
            v-if="item.prop === 'firmName'"
            v-model="form.firmName"
          ></el-input>
          <el-input
            v-if="item.prop === 'firmCode'"
            v-model="form.firmCode"
          ></el-input>
          <el-input
            v-if="item.prop === 'emailAddress'"
            v-model="form.emailAddress"
          ></el-input>
          <el-input
            v-if="item.prop === 'postalCode'"
            v-model="form.postalCode"
          ></el-input>
          <el-select
            v-if="item.prop === 'fundSupplyModeName'"
            v-model="form.fundSupplyModeName"
            placeholder="请选择经费供给方式"
          >
            <el-option
              v-for="item in fundSupplyModeNameOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            v-if="item.prop === 'fundSupplyModeCode'"
            v-model="form.fundSupplyModeCode"
          ></el-input>
          <el-select
            v-if="item.prop === 'administrationLevelName'"
            v-model="form.administrationLevelName"
            placeholder="请选择单位行政级别名称"
          >
            <el-option
              v-for="item in administrationLevelNameOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            v-if="item.prop === 'financialAffiliation'"
            v-model="form.financialAffiliation"
          ></el-input>
          <el-select
            v-if="item.prop === 'accountingSystem'"
            v-model="form.accountingSystem"
            placeholder="请选择单位执行会计制度"
          >
            <el-option
              v-for="item in accountingSystemOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            v-if="item.prop === 'institutionNumber'"
            v-model="form.institutionNumber"
          ></el-input>
          <el-input
            v-if="item.prop === 'authorizedNumber'"
            v-model="form.authorizedNumber"
          ></el-input>
          <el-input
            v-if="item.prop === 'ofSideNumber'"
            v-model="form.ofSideNumber"
          ></el-input>
          <el-input
            v-if="item.prop === 'institutionAuthority'"
            v-model="form.institutionAuthority"
          ></el-input>
          <el-input
            v-if="item.prop === 'administrationAuthority'"
            v-model="form.administrationAuthority"
          ></el-input>
          <el-select
            v-if="item.prop === 'unitReform'"
            v-model="form.unitReform"
            placeholder="请选择事业单位改革分类"
          >
            <el-option
              v-for="item in unitReformOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select
            v-if="item.prop === 'personExpenditure'"
            v-model="form.personExpenditure"
          >
            <el-option
              v-for="item in personExpenditureOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        style="margin-left: 88%; margin-right: 20px"
        @click="handleCloseDialog"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import {
  organizationOptions,
  unitTypeOption,
  accountingSystemOption,
  administrationLevelNameOption,
  budgetManagementLevelOption,
  budgetUnitNameOption,
  fundSupplyModeNameOption,
  unitBasicPropertyOption,
  unitReformOption,
  personExpenditureOption,
  isVirtualOptions,
} from "../data";
import _ from "lodash";
import { unitUpdate } from "@api/tenant";
import { mapGetters } from "vuex";
import { areaData } from "@utils/unitArea.js";
import { isvalidatemobile } from "@utils/validate";
import { validateSocialCreCode } from "@utils/validate";
export default {
  name: "TheAppDialog",
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (this.form.isVirtual == "0") {
        if (value.length !== 18) {
          callback(new Error("统一社会信用代码格式不正确"));
        } else {
          callback();
        }
      }else{
        callback();
      }
    };
    return {
      options: {
        labelWidth: "100px",
      },
      formItem: [
        {
          label: "单位名称",
          prop: "unitName",
        },
        {
          label: "联系方式",
          prop: "linkPhone",
        },
        {
          label: "法人代表",
          prop: "corporateRepresentative",
        },
        {
          label: "单位编码",
          prop: "unitCode",
        },
        {
          label: "区划编码",
          prop: "administrationDivisionCode",
        },
        {
          label: "区划名称",
          prop: "administrationDivisionName",
        },
        {
          label: "统一社会信用代码",
          prop: "socialCreditCode",
        },
        {
          label: "实/虚单位",
          prop: "isVirtual",
        },
        {
          label: "单位地址",
          prop: "value",
        },
        {
          label: "详细地址",
          prop: "detailAddress",
        },
        {
          label: "组织形式",
          prop: "organizationForm",
        },
        {
          label: "单位分类",
          prop: "unitType",
        },
        {
          label: "本级财政代码",
          prop: "localFinancialCode",
        },
        {
          label: "本级财政名称",
          prop: "localFinancialName",
        },
        {
          label: "本级机关事务管理局代码",
          prop: "officeAdministrationCode",
        },
        {
          label: "本级机关事务管理局名称",
          prop: "officeAdministrationName",
        },
        {
          label: "财政预算代码",
          prop: "budgetCode",
        },
        {
          label: "单位类型名称",
          prop: "unitTypeName",
        },
        {
          label: "单位基本性质",
          prop: "unitBasicProperty",
        },
        {
          label: "事业单位改革分类",
          prop: "unitReform",
        },
        {
          label: "预算单位级次名称",
          prop: "budgetUnitName",
        },
        {
          label: "预算管理级次",
          prop: "budgetManagementLevel",
        },
        {
          label: "行业名称",
          prop: "firmName",
        },
        {
          label: "行业代码",
          prop: "firmCode",
        },
        {
          label: "电子邮件",
          prop: "emailAddress",
        },
        {
          label: "邮政编码",
          prop: "postalCode",
        },
        {
          label: "经费供给方式",
          prop: "fundSupplyModeName",
        },
        {
          label: "经费供给方式代码",
          prop: "fundSupplyModeCode",
        },
        {
          label: "基本支出人员经费功能分类",
          prop: "personExpenditure",
        },
        {
          label: "单位行政级别名称",
          prop: "administrationLevelName",
        },
        {
          label: "财务隶属关系",
          prop: "financialAffiliation",
        },
        {
          label: "单位执行会计制度",
          prop: "accountingSystem",
        },
        {
          label: "事业编制人数",
          prop: "institutionNumber",
        },
        {
          label: "行政编制人数",
          prop: "authorizedNumber",
        },
        {
          label: "编制外人数",
          prop: "ofSideNumber",
        },
        {
          label: "事业编制",
          prop: "institutionAuthority",
        },
        {
          label: "行政编制",
          prop: "administrationAuthority",
        },
      ],
      flag: false,
      form: {},
      province: "",
      city: "",
      street: "",
      detailAddress: "",
      imageUrl: "",
      provinces: [],
      citys: [],
      streets: [],
      administrationDivisionName: [],
      value: [],
      area: [],
      propsName: { expandTrigger: "hover", checkStrictly: true },
      props: { expandTrigger: "hover" },
      rules: {
        socialCreditCode: [{ validator: validatePass, trigger: "blur" }],
      },
      organizationOptions:[],
      unitTypeOption:[],
      accountingSystemOption:[],
      administrationLevelNameOption:[],
      budgetManagementLevelOption:[],
      budgetUnitNameOption:[],
      fundSupplyModeNameOption:[],
      unitBasicPropertyOption:[],
      unitReformOption:[],
      personExpenditureOption:[],
      isVirtualOptions:[],
      // view
      loading: false,
    };
  },
  props: {
    dialogShow: {
      type: Object,
      default: () => {},
    },
    tenantInfo: {
      type: Object,
      default: () => {},
    },
  },
  created() {
    this.$nextTick(() => {
      this.changeArea();
      this.init();
      this.changeUnitInfo();
    });
  },
  computed: {},
  methods: {
    changeUnitInfo() {
      /**
       * 处理一下各个select框，其值为-1时为选择框
       */
      if (this.form.organizationForm == -1) {
        this.form.organizationForm = "";
      }
      if (this.form.unitType == -1) {
        this.form.unitType = "";
      }
      if (this.form.accountingSystem == -1) {
        this.form.accountingSystem = "";
      }
      if (this.form.budgetManagementLevel == -1) {
        this.form.budgetManagementLevel = "";
      }
      if (this.form.budgetUnitName == -1) {
        this.form.budgetUnitName = "";
      }
      if (this.form.unitBasicProperty == -1) {
        this.form.unitBasicProperty = "";
      }
      if (this.form.isVirtual == -1) {
        this.form.isVirtual = "1";
      }
      /**
       * 处理一下option值
       */
      this.organizationOptions = organizationOptions
      this.unitTypeOption = unitTypeOption
      this.accountingSystemOption = accountingSystemOption
      this.administrationLevelNameOption = administrationLevelNameOption
      this.budgetManagementLevelOption = budgetManagementLevelOption
      this.budgetUnitNameOption = budgetUnitNameOption
      this.fundSupplyModeNameOption = fundSupplyModeNameOption
      this.unitBasicPropertyOption = unitBasicPropertyOption
      this.unitReformOption = unitReformOption
      this.personExpenditureOption = personExpenditureOption
      this.isVirtualOptions = isVirtualOptions
    },
    init() {
      /**
       * 处理一下street 分成区和详细地址 能显示在输入框中
       */
      let data = _.cloneDeep(this.tenantInfo);
      console.log("data", data);
      this.form = data;
      this.province = data.province;
      this.city = data.city;
      let address = "";
      address = data.streetAddress;
      if (data.administrationDivisionName !== "") {
        this.administrationDivisionName = data.administrationDivisionName.split(
          ","
        );
      }
      if (address != "") {
        let index = address.indexOf("区");
        if (index === -1) {
          index = address.indexOf("县");
        }
        let street = address.substring(0, index + 1);
        this.street = street;
        let detailAddress = address.substring(index + 1, address.length);
        this.detailAddress = detailAddress;
        if (this.street !== "") {
          this.value = [this.province, this.city, this.street];
        } else {
          this.value = [this.province, this.city];
        }
      } else {
        this.street = "";
        this.detailAddress = "";
      }
    },
    changeArea() {
      areaData.forEach((el, index) => {
        this.area.push({
          label: el.province,
          value: el.province,
          children: [],
        });
        el.children.forEach((xl, ol) => {
          if (xl.children) {
            this.area[index].children.push({
              label: xl.name,
              value: xl.name,
              children: [],
            });
            xl.children.forEach((al) => {
              this.area[index].children[ol].children.push({
                label: al,
                value: al,
              });
            });
          } else {
            this.area[index].children.push({
              label: xl.name,
              value: xl.name,
            });
          }
        });
      });
      this.provinces = this.area;
    },
    handleCloseDialog() {
      this.$emit("closeDialog", this.dialogShow.key);
    },
    handleChange(val) {
      if (val.length == 3) {
        this.province = val[0];
        this.city = val[1];
        this.street = val[2];
      } else {
        this.province = val[0];
        this.city = val[1];
      }
    },
    handleSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$loading({ fullscreen: true, text: "修改中" });
          this.form.sort = 0;
          let street = this.street;
          let detailAddress = this.detailAddress;
          let streetAddress = street + detailAddress;
          let administrationDivisionName = this.administrationDivisionName;
          administrationDivisionName = administrationDivisionName.join(",");
          this.form.administrationDivisionName = administrationDivisionName;
          this.form.streetAddress = streetAddress;
          this.form.province = this.province;
          this.form.city = this.city;
          console.log("thifrm", this.form);
          unitUpdate(this.form)
            .then((res) => {
              console.log("res", res);
              if (res.data.code == "200") {
                this.$message.success("修改成功");
                setTimeout(() => {
                  this.$loading().close();
                }, 500);
                location.reload();
                this.$emit("freshUnitInfo");
                this.$emit("closeDialog", "editTenant");
              }
            })
            .catch(() => {
              setTimeout(() => {
                this.$loading().close();
              }, 500);
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100% !important;
}
::v-deep .el-dialog__body {
  overflow: auto;
}
.diy-dialog-body {
  justify-content: space-between;
  .el-form {
    justify-content: space-between;
    .el-input {
      width: 250px;
    }
  }
}
.diy-dialog-footer .el-button:first-child {
  margin-right: 10px;
}
.body-container {
  margin-top: 10px;

  &__title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
    color: rgba(48, 49, 51, 1);
  }
}
.body-item {
  margin-top: 10px;

  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  &-main {
    width: 100%;
    margin-top: 10px;
  }

  .diy-table {
    padding: 0;
  }
}
</style>

<style lang="scss" scoped>
.body-container ::v-deep .el-form {
  .el-cascader,
  .el-input {
    width: 350px;
  }
}
::v-deep .el-textarea__inner {
  height: 200px;
}
::-webkit-scrollbar {
  width: 8px;
  height: 7px;
  background: #ffffff;
}
::-webkit-scrollbar-thumb {
  height: 0px;
  border-radius: 4px;
  background: #90939928;
}
::-webkit-scrollbar-thumb:hover {
  width: 8px;
  background: #90939967;
}
::v-deep.diy-dialog-body {
  border: 0 !important;
}
::v-deep .el-tabs__header {
  margin: 0px !important;
}
::v-deep .el-dialog__body {
  height: 556px;
}
.progress-box {
  width: fit-content;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.tabs {
  width: 490px;
}
.diy-dialog-footer {
  margin-top: 120px;
}
.diy-dialog-box {
  border: 1px solid rgba(235, 238, 245, 1);
  padding: 10px;
  display: flex;
  flex-direction: column;
  &__checkBox {
    border: 1px solid rgba(235, 238, 245, 1);
    padding: 10px;
  }
  &__radio {
    margin-top: 10px;
    border: 1px solid rgba(235, 238, 245, 1);
    padding: 10px;
  }
  &-treeBox {
    display: flex;
    flex-direction: row;
    margin-top: 5px;
  }
  &-left,
  &-right {
    width: 50%;
    padding: 16px;
    border: 1px solid rgba(235, 238, 245, 1);
  }
}
.layout-role {
  width: 100%;
  padding: 10px;
  border: 1px solid rgba(235, 238, 245, 1);
}
.diy-dialog-body {
  border: 1px solid rgba(235, 238, 245, 1);
  display: flex;
  flex-direction: column;
  border-radius: 4px;
  justify-content: flex-start;
  &--left,
  &--right {
    width: 50%;
    height: 500px;
    padding: 16px;
  }

  &--left {
    border-right: 1px solid rgba(235, 238, 245, 1);
  }

  &__title {
    font-size: 14px;
    font-weight: 500;
    height: 20px;
    color: rgba(48, 49, 51, 1);
  }

  &__list {
    height: 440px;
    margin-top: 10px;
    overflow-y: scroll;

    li {
      overflow: hidden;
      height: 26px;
      line-height: 26px;
      display: flex;
      justify-content: space-between;

      i {
        margin-right: 5px;
        color: #c0c4cc;
        cursor: pointer;

        &:hover {
          color: #9da1a8;
        }
      }
    }
  }
}
::v-deep .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /*height:600px;*/
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
</style>
